<template>
  <div>
    <el-card class="login-form-layout">
      <el-form
        autocomplete="on"
        :model="loginForm"
        ref="loginForm"
        label-position="left">
        <h2 class="login-title color-main">Login</h2>
        <el-form-item prop="username">
          <el-input
            name="username"
            type="text"
            v-model="loginForm.username"
            autocomplete="on"
            placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :type="pwdType"
            @keyup.enter.native="handleLogin"
            v-model="loginForm.password"
            autocomplete="on"
            placeholder="请输入密码">
          </el-input>
        </el-form-item>
        <el-form-item style="margin-bottom: 60px">
          <el-button
            style="width: 100%"
            type="primary"
            :loading="loading"
            @click.native.prevent="handleLogin">登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'WsLogin',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loading: false,
      pwdType: 'password'
    }
  },
  methods: {
    handleLogin () {
      this.$refs.loginForm.validate(value => {
        if (!value) {
          console.log('参数验证不合法！')
          return false
        }

        this.loading = true
        this.$store.dispatch('login', this.loginForm)
          .then(response => {
            this.loading = false
            if (response.data === true) {
              this.$router.push({
                path: '/wshow',
                query: { data: response.data.data }
              })
            }
          })
          .catch(() => {
            this.loading = false
          })
      })
    }
  }
}
</script>
